﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace BHExtend.Interface
{
    public class SendEmail
    {

        /// <summary>
        /// 通过SMTP协议发送邮件
        /// 作者：周驰
        /// 日期：2018-10-08
        /// </summary>
        /// <param name="smtpService">smtp服务器地址，用以实例化一个smtp客户端，如：“mail.zlsoft.cn”</param>
        /// <param name="sendEmail">发件邮箱地址，如“xxx@zlsoft.cn”</param>
        /// <param name="sendpwd">发件邮箱地址密码</param>
        /// <param name="recAddress">收件邮箱地址，多个邮箱地址间用“；”隔开</param>
        /// <param name="ccAddress">抄送邮箱地址，多个邮箱地址间用“；”隔开</param>
        /// <param name="mailSubject">邮件标题</param>
        /// <param name="mailBody">邮件内容</param>
        /// <param name="send_status">邮件发送结果，成功返回true，失败返回false</param>
        /// <returns></returns>
        public static string sendingMail(string smtpService, string sendEmail, string sendpwd, string recAddress, string ccAddress, string mailSubject, string mailBody, out bool send_status)
        {
            #region 处理主送抄送列表

            string[] recAddress_list = recAddress.Split(';');
            string[] ccAddress_list = ccAddress.Split(';');
            #endregion

            #region 发送邮件

            //确定smtp服务器地址 实例化一个Smtp客户端
            SmtpClient smtpclient = new SmtpClient();
            smtpclient.Host = smtpService;
            //smtpClient.Port = "";//qq邮箱可以不用端口



            //构造一个Email的Message对象 内容信息
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(sendEmail);
            mailMessage.Subject = mailSubject;
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
            mailMessage.Body = mailBody;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

            //是否以html形式发送
            mailMessage.IsBodyHtml = true;

            //处理收件邮箱列表
            foreach (var item in recAddress_list)
            {
                if (item != "")
                    mailMessage.To.Add(new MailAddress(item));
            }

            //处理抄送列表
            foreach (var item in ccAddress_list)
            {
                if (item != "")
                    mailMessage.CC.Add(new MailAddress(item));
            }
            //邮件发送方式  通过网络发送到smtp服务器
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;

            //如果服务器支持安全连接，则将安全连接设为true。QQ邮箱需要ssl安全连接，但mail.zlsoft.cn不需要
            if (smtpService == "smtp.qq.com")
            {
                smtpclient.EnableSsl = true;
            }
            try
            {
                //是否使用默认凭据，若为false，则使用自定义的证书，就是下面的networkCredential实例对象
                smtpclient.UseDefaultCredentials = false;

                //指定邮箱账号和密码
                NetworkCredential networkCredential = new NetworkCredential(sendEmail, sendpwd);
                smtpclient.Credentials = networkCredential;

                //发送邮件
                smtpclient.Send(mailMessage);
                // Console.WriteLine("发送邮件成功");
                send_status = true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                //Console.WriteLine(ex.Message, "发送邮件出错");
                send_status = false;
                return ex.Message;
            }
            #endregion

            return "邮件发送成功!";
        }

    }
}
